
#include <stdio.h>


#include <cv.h>
#include <highgui.h>
#include <ml.h>
#include "resourceTracker.h"
#include "handLocaliser.hpp"


#include "opencv2/highgui/highgui.hpp"
#include "opencv2/core/core.hpp"
#include "opencv2/imgproc/imgproc.hpp"


/*
 *  GestureClassifier_scrap.cpp
 *  
 *
 *  Created by Rasmus Kyng on 11/01/2011.
 *  Copyright 2011 __MyCompanyName__. All rights reserved.
 *
 */


//class GestureClassifier {
//public:
//	train( CvDTreeTrainData*
//
//}

using namespace cv;

ResourceTracker* rt;
HandLocaliser* hl;
cv::Ptr< cv::VideoCapture > capPtr;
cv::Ptr< VideoPointTagSequence > seqPtr;

Rect handRect;

Mat frame;

void initTest(const char* videoNameString) {
	
	rt = new ResourceTracker( "../config/runtimeSettings/cfg.xml" );
	rt->loadCategory("TestData");
	
	capPtr = rt->getFileVideoCapture( videoNameString );
	seqPtr = rt->getPointTags( videoNameString );
	
	(*capPtr) >> frame;
	
	hl = new HandLocaliser();
	
	handRect = hl->localiseHand( frame );
	
	//forcing rectangle to stay inside frame
	//FIX: this approach is simplistic?
	//used clipped frame instead?
	//maybe not, this approach does seem quite easy to use as user
	if ( handRect.x < 0 ) handRect.x = 0;
	if ( frame.cols < handRect.x + handRect.width ) handRect.x = frame.cols - handRect.width;
	if ( handRect.y < 0 ) handRect.y = 0;
	if ( frame.rows < handRect.y + handRect.height ) handRect.y = frame.rows - handRect.height;
}


int main(int argc, const char* argv[]) {
	
	const char* videoNameString;
	
	if (argc == 1) {
		videoNameString = "locVid1c";
	} else {
		videoNameString = argv[1];
	}
	
	//Trying convexity defect first
	//CvBoost* boostClassifier = new CvBoost();

	
	initTest( videoNameString );

	int tagCount = seqPtr->tagCount;
	
	// -- visualisation: set up -- //
	
	char* windows[] = { "classifier scrap", "hand rect", "psmf", "flood filled" };
	int wc = 4;
	
	//Note: using OpenCV/C function as this has not been implemented in OpenCV/C++
	
	int ybase = 100;
	int yskip = handRect.y;
	
	namedWindow( windows[0], 1);
	cvMoveWindow( windows[0], 0, ybase);

	for (int i = 1; i<wc; i++) {
		namedWindow( windows[i], 1);
		cvMoveWindow( windows[i], 700, ybase+(i-1)*yskip);
	}
	
//	cvMoveWindow( mainWindowName, 0, ybase);
//	cvMoveWindow( handFrameWindowName, 700, ybase );
//	cvMoveWindow( pmsfWindowName, 700, ybase + 1*yskip );
//	cvMoveWindow( floodFillWindowName, 700, ybase + 2*yskip );
//	cvMoveWindow( contourWindowName, 700, ybase + 3*yskip );
	

	
	for( int frameCount = 0; frameCount<tagCount; frameCount++) {
		
		
		
		// -- tracking frame -- //
		Point groundTruth = seqPtr->getNextPointTag();
		//handRect = Rect( groundTruth.x - handRect.width/2, groundTruth.y - handRect.height/2, handRect.width, handRect.height );
		handRect = Rect( groundTruth.x - handRect.width/2, groundTruth.y - handRect.height/2, handRect.width, handRect.height );
		
		
		//forcing rectangle to stay inside frame
		if ( handRect.x < 0 ) handRect.x = 0;
		if ( frame.cols < handRect.x + handRect.width ) handRect.x = frame.cols - handRect.width;
		if ( handRect.y < 0 ) handRect.y = 0;
		if ( frame.rows < handRect.y + handRect.height ) handRect.y = frame.rows - handRect.height;
		
		
		// -- segmentation on hand ROI -- //
		
		Mat handImg(frame, handRect);
		
		Mat handPMSFImg;

		int spatialRad = 10;
		int colorRad = 10;
		int maxPyrLevel = 1;
		
		pyrMeanShiftFiltering( handImg, handPMSFImg, spatialRad, colorRad, maxPyrLevel );
		
//		dilate( handEdgeImg,handEdgeImg, Mat(), Point(-1, -1), 1);
//		
//		vector<vector<Point> > contours;
//		vector<Vec4i> hierarchy;
//		
//		findContours( handEdgeImg, contours, hierarchy, CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE );
//		
//		Mat contoursImg = Mat::zeros(handEdgeImg.size(), CV_8UC3);
		
		//
		
		Mat floodFilled;
		
		Mat mask( handPMSFImg.rows+2, handPMSFImg.cols+2, CV_8UC1, Scalar::all(0) );
		
		handPMSFImg.copyTo( floodFilled );
		
		Scalar colourDiff = Scalar::all(5);
		
		floodFill( floodFilled, mask, Point(groundTruth.x-handRect.x, groundTruth.y-handRect.y),
											Scalar::all(255), 0, colourDiff, colourDiff );
		
			
		// -- visualisation: display -- //
		
		{
			rectangle( frame, Point(handRect.x, handRect.y),
					  Point(handRect.x + handRect.width, handRect.y + handRect.height),
					  Scalar::all(250));
			
			circle( frame, groundTruth, 1, Scalar(0, 255, 0), 2 ); //radius 1, thickness 2, colour green
			//NOTE: BGR colour!
			
			
//			if( contours.size() > 0 )
//			{
//				// iterate through all the top-level contours,
//				// draw each connected component with its own random color
//				int idx = 0;
//				for( ; idx >= 0; idx = hierarchy[idx][0] )
//				{
//					Scalar color( (rand()&255)+10, (rand()&255)+10, (rand()&255)+10 );
//					drawContours( contoursImg, contours, idx, color, CV_FILLED, 8, hierarchy );
//				}
//			}
			
			
			imshow( windows[0], frame );
			imshow( windows[1], handImg );
			imshow( windows[2], handPMSFImg );
			imshow( windows[3], floodFilled );
			//imshow( contourWindowName, contoursImg );
			

			
			
		}
		
		// -- get next frame -- //
		(*capPtr) >> frame;
		
		waitKey(1);
	}
	

	
	delete rt;
	delete hl;
	
	//delete boostClassifier;

	return 0;

}